package com.sopt.friends;

import java.util.ArrayList;

import com.sopt.friends.db.FriendsDBHelper;
import com.sopt.friends.db.Constants.FriendsDB;
import com.sopt.friends.statics.FriendsStaticVars;
import com.sopt.friends.util.FriendsContactsManager;
import com.sopt.friends.vo.FriendsBasicMember;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private SQLiteDatabase sdb;
	private FriendsDBHelper dbhelper;
	private FriendsContactsManager manager;
	private ArrayList<String> resultUpdateMembers;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
	}
	private void init(){
		dbhelper = new FriendsDBHelper(this);
		sdb = dbhelper.getReadableDatabase();

		manager = new FriendsContactsManager(this, getContentResolver());

		Cursor c = sdb.query(FriendsDB.TABLE_GROUP, null, null, null, null,
				null, null);

		startManagingCursor(c);

		if (c.getCount() == 0) {
			appendInitData();

			ArrayList<FriendsBasicMember> list = manager.getContactsList();
			int resultSetInitMembersCount = manager
					.setInitDatabaseContactList(list);

		} else {
			// Check for the update Contacts
			resultUpdateMembers = manager.setFriendsMemberUpdate();
		}
		c.close();
		sdb.close();

		String myNumber = "CANNOT ACCESS";
		TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		myNumber = mTelephonyMgr.getLine1Number();

		if (myNumber.equals("CANNOT ACCESS") || myNumber.length() < 1) {
			FriendsStaticVars.CURRENT_USER_NUMBER = "0000";
		} else {
			FriendsStaticVars.CURRENT_USER_NUMBER = myNumber;
		}
		//Log.v("changdoc", "LOADING COMPLETE :: my Number = "+ FriendsStaticVars.CURRENT_USER_NUMBER);
	}

	private void toMainActivity() {
		startActivity(new Intent(this, FirstActivity.class));
	}

	@Override
	protected void onResume() {
		super.onResume();
		init();
		
		Toast.makeText(this, getResources().getString(R.string.mainTargetText), Toast.LENGTH_LONG).show();
		View view = (View) findViewById(R.id.mainTarget);
		if (view != null) {
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					toMainActivity();

				}
			});
		}
	}

	private void appendInitData() {
		sdb = dbhelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(FriendsDB.GROUP_NAME, "ALL");
		sdb.insert(FriendsDB.TABLE_GROUP, null, values);
		sdb.close();
	}

}